package top.eggcode.modules.dict.control;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.eggcode.common.mvc.Result;
import top.eggcode.common.mvc.ResultWrapper;
import top.eggcode.modules.dict.model.DictForm;
import top.eggcode.modules.dict.model.DictQuery;
import top.eggcode.modules.dict.model.DictView;
import top.eggcode.modules.dict.service.DictService;
import top.eggcode.base.BaseController;

/**
 * Title: 字典类型访问接口
 * Description: TODO
 * Date: 2021/5/21 20:49
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Api(tags = "词典")
@RequiredArgsConstructor
@RequestMapping("/api/dict")
@RestController
public class DictController extends BaseController<DictForm, DictQuery> {

    private final DictService selfService;

    @GetMapping("/{id}")
    @Override
    public Result<?> get(@PathVariable String id) {
        return super.pack(selfService.get(id).get(), DictView.class);
    }

    @GetMapping("/list/page")
    @Override
    public Result<?> listPage(DictQuery query) {
        return super.pack(selfService.page(query), DictView.class);
    }

    @DeleteMapping("/{id}/delete")
    @Override
    public Result<?> delete(@PathVariable String id) {
        selfService.deleteById(id);
        return ResultWrapper.successful();
    }

    @PatchMapping("/update")
    @Override
    public Result<?> update(DictForm view) {
        selfService.update(view.convert());
        return null;
    }

    @PostMapping("/add")
    @Override
    public Result<?> add(@RequestBody DictForm view) {
        selfService.put(view.convert());
        return ResultWrapper.successful();
    }

    @PostMapping("/getWords")
    public Result<?> getWords(@RequestParam("code") String code) {

        return ResultWrapper.successful(selfService.getData(code));
    }
}
